var jsonList = {};
var page = 1;
var rows = 10;
$(".dp_qx").on("click",function (){
	$('.operation').show();
	$('.table_border').show();
	$('.cont1').hide();
	fn();
	$(this).addClass('active');
})
$('.tianjia').on('click',function(){
	$('.operation').hide();
	$('.table_border').hide();
	$('.cont1').show();
	$(".dp_add").show();
	$(".dp_xg").hide();
	fn();
	$(this).addClass('active');
})

// $('.xiugai').on('click',function(){
// 	$('.operation').hide();
// 	$('.table_border').hide();
// 	$('.cont1').show();
// 	fn();
// 	$(this).addClass('active');
// })
function fn(){
		$('.title div').removeClass('active');
}
$(function (){
	//获取短评列表
	getList();
	$(".dp_add").on("click",function (){
		var dp_zuozhe = $(".dp_zuozhe").val();//作者
		var dp_laiyuan = $(".dp_laiyuan").val();//来源
		var dp_wangzhi = $(".dp_wangzhi").val();//网址
		var dp_leibie =$(".dp_leibie").val();//短评类别
		//var dp_duanti = $(".dp_duanti").val();//专题类别
		var dp_duanti=$("#lunch").val();
		var dp_biaoti = $(".dp_biaoti").val();//包体
		var content = editor.txt.html();//内容
		var fbs_text = $(".fbs_text").val();//发布时间
		var arcImg = $(".tu img").attr("fsrc")||"";//封面图
		if (arcImg==""){
			zeroModal.alert('请选择封面图片');
			return false;
		}
		var json = {
			"author": dp_zuozhe,
			"content": content,
			"sid": dp_duanti,
			"source": dp_laiyuan,
			"title": dp_biaoti,
			"type": dp_leibie,
			"url": dp_wangzhi,
			"cover":arcImg
		}
		if (fbs_text!=""){
			json.stime = fbs_text + ":00"
		}
		console.log(json, 9999)
		var result = JSON.stringify(json);
		zeroModal.loading(5);
		
		$.ajax({
			url: baseNews + "/api/data/sr/add",
			type: "post",
			headers: { 'Content-Type': 'application/json' },
			dataType: "json",
			data: result,
			success: function (res) {
				console.log(res, 666)
				if (res.code == 0) {
					location.reload();
				} else {

					zeroModal.alert('添加失败');

				}
			}
		})

	})
	//短评删除
	$(".pd_shanchu").on('click',function (res){
		var numCount = 0;
		var ids = '';
		$(".dp_chk").each(function (index, key) {
			if ($(this).prop("checked") == true) {
				numCount++;
				ids = ids + $(this).attr("oid") + ',';
			}
		})
		console.log(numCount, 555);
		if (numCount >= 1) {
			//进行下一步
			var json = {}
			var result = JSON.stringify(json);
			zeroModal.confirm("确定要删除该短评吗？", function () {
				//置顶确认请求数据
				$.ajax({
					url: baseNews + "/api/data/sr/del?ids=" + ids,
					type: "post",
					headers: { 'Content-Type': 'application/json' },
					dataType: "json",
					data: result,
					success: function (res) {
						if (res.code == 0) {
							location.reload();
						} else {
							zeroModal.alert('删除短评失败');
						}
					}
				})
			});
		} else {
			//遮罩层提示
			zeroModal.alert('请选择至少一条短评删除');

		}
	})
	//短评置顶
	$(".dp_zhiding").on("click",function (){
		var numCount = 0;
		var ids = '';
		$(".dp_chk").each(function (index, key) {
			if ($(this).prop("checked") == true) {
				numCount++;
				ids = ids + $(this).attr("oid") + ',';
			}
		})
		console.log(numCount, 555);
		if (numCount >= 1) {
			//进行下一步
			var json = {}
			var result = JSON.stringify(json);
			zeroModal.confirm("确定要置顶该短评吗？", function () {
				//置顶确认请求数据
				$.ajax({
					url: baseNews + "/api/data/sr/top/true?ids=" + ids,
					type: "post",
					headers: { 'Content-Type': 'application/json' },
					dataType: "json",
					data: result,
					success: function (res) {
						if (res.code == 0) {
							location.reload();
						} else {
							zeroModal.alert('短评置顶失败');
						}
					}
				})
			});
		} else {
			//遮罩层提示
			zeroModal.alert('请选择至少一条短评置顶');

		}
	})
	//取消短评置顶
	$(".dp_qxzd").on("click", function () {
		var numCount = 0;
		var ids = '';
		$(".dp_chk").each(function (index, key) {
			if ($(this).prop("checked") == true) {
				numCount++;
				ids = ids + $(this).attr("oid") + ',';
			}
		})
		console.log(numCount, 555);
		if (numCount >= 1) {
			//进行下一步
			var json = {}
			var result = JSON.stringify(json);
			zeroModal.confirm("确定要取消置顶该短评吗？", function () {
				//置顶确认请求数据
				$.ajax({
					url: baseNews + "/api/data/sr/top/false?ids=" + ids,
					type: "post",
					headers: { 'Content-Type': 'application/json' },
					dataType: "json",
					data: result,
					success: function (res) {
						if (res.code == 0) {
							location.reload();
						} else {
							zeroModal.alert('取消置顶失败');
						}
					}
				})
			});
		} else {
			//遮罩层提示
			zeroModal.alert('请选择至少一条取消短评置顶');

		}
	})
	//短评推荐
	$(".dp_tj").on("click", function () {
		var numCount = 0;
		var ids = '';
		$(".dp_chk").each(function (index, key) {
			if ($(this).prop("checked") == true) {
				numCount++;
				ids = ids + $(this).attr("oid") + ',';
			}
		})
		console.log(numCount, 555);
		if (numCount >= 1) {
			//进行下一步
			var json = {}
			var result = JSON.stringify(json);
			zeroModal.confirm("确定要推荐短评吗？", function () {
				//置顶确认请求数据
				$.ajax({
					url: baseNews + "、api/data/sr/recommend/true?ids=" + ids,
					type: "post",
					headers: { 'Content-Type': 'application/json' },
					dataType: "json",
					data: result,
					success: function (res) {
						if (res.code == 0) {
							location.reload();
						} else {
							zeroModal.alert('推荐失败');
						}
					}
				})
			});
		} else {
			//遮罩层提示
			zeroModal.alert('请选择至少一条短评推荐');

		}
	})
	//取消短评推荐
	$(".dp_qxtj").on("click", function () {
		var numCount = 0;
		var ids = '';
		$(".dp_chk").each(function (index, key) {
			if ($(this).prop("checked") == true) {
				numCount++;
				ids = ids + $(this).attr("oid") + ',';
			}
		})
		console.log(numCount, 555);
		if (numCount >= 1) {
			//进行下一步
			var json = {}
			var result = JSON.stringify(json);
			zeroModal.confirm("确定要取消推荐短评吗？", function () {
				//置顶确认请求数据
				$.ajax({
					url: baseNews + "、api/data/sr/recommend/false?ids=" + ids,
					type: "post",
					headers: { 'Content-Type': 'application/json' },
					dataType: "json",
					data: result,
					success: function (res) {
						if (res.code == 0) {
							location.reload();
						} else {
							zeroModal.alert('取消推荐失败');
						}
					}
				})
			});
		} else {
			//遮罩层提示
			zeroModal.alert('请选择至少一条短评取消推荐');

		}
	})
	//获取类别
	$.ajax({
		url: baseNews + "/api/data/sr/types",
		success: function (res) {
			console.log(res);
			if (res.code == 0) {
				var obj = $(".dp_leibie");
				$(res.data).each(function (index, key) {
					obj.append("<option value='" + key.id + "'>" + key.value + "</option>");				})
			}
		}
	})
	//获取专题id
	$.ajax({
		url: baseNews + "/api/data/sr/special/select",
		success: function (res) {
			console.log(res);
			if (res.code == 0) {
				var obj = $(".select_zt");
				$(res.data).each(function (index, key) {
				$(".selectpicker").append("<option value='" + key.id + "'>" + key.value + "</option>");	})
				$('#lunch').selectpicker('refresh');
				$('#lunch').selectpicker('render');  
			}
		}
	})
	
	//修改  获取详情
	$(".btn_sg").on("click",function (){
		$(".dp_xg").show();
		$(".dp_add").hide();
		var numCount = 0;
		var ids = '';
		$(".dp_chk").each(function (index, key) {
			if ($(this).prop("checked") == true) {
				numCount++;
				ids = ids + $(this).attr("oid");
			}
		})
		if (numCount == 1) {
			//进行下一步
			$('.operation').hide();
			$('.table_border').hide();
			$('.cont1').show();
			console.log(ids, 898989);
			var json = {}
			var result = JSON.stringify(json);
			$.ajax({
				url: baseNews + "/api/data/sr/info",
				data: {
					id: ids
				},
				success: function (res) {
					//console.log(res,7878)
					if (res.code == 0) {
						var result = res.data;
						console.log(result);
						$(".dp_id").val(ids);
						$(".dp_zuozhe").val(result.author);//作者
						$(".dp_laiyuan").val(result.source);//来源
						$(".dp_wangzhi").val(result.url);//网址
						//$(".dp_leibie").val();//短评类别
						//$(".dp_duanti").val();//专题类别
						$('#lunch').val([result.sid]);
				$('#lunch').selectpicker('refresh');
				$('#lunch').selectpicker('render'); 
						$(".dp_biaoti").val(result.title);//包体
						 //editor.txt.html();//内容
						 $("#demo-2").val(result.stime);
						 if (result.cover!=""){
							$(".tu img").attr("src", result.cover);
							$(".tu img").attr("fsrc", result.cover);
						}
						editor.txt.append(result.content);
						
					}
				}
			})

		} else {
			//遮罩层提示
			zeroModal.alert("请选择一条数据进行修改");
			return false;

		}
	})
})
function getList() {
	var result = JSON.stringify(jsonList);
	$.ajax({
		url: baseNews + "/api/data/sr/list?page=" + page + "&rows=" + rows,
		type: "post",
		headers: { 'Content-Type': 'application/json' },
		dataType: "json",
		data: result,
		success: function (res) {
			console.log(res, 666);
			if (res.code == 0) {
				var result = res.data;
				var list = result.list;
				console.log(list, 9999)
				var html = "";
				$("#page").paging({
					pageNo: page,
					totalPage: result.totalpage,
					totalSize: result.totals,
					callback: function (num) {
						page = num;
						getList();
					}
				})
				$(".dp_list").empty();
				$(list).each(function (index, key) {
					var state = key.state;//短评的状态
					var newState = "";//短评的
					var state1 = key.sstate;//专题的状态
					var newState1 = "";//文章的
					if (state == 1) {
						newState = "待审核";
					}
					if (state == 2) {
						newState = "待发布";
					}
					if (state == 3) {
						newState = "已发布";
					}
					if (state == -1) {
						newState = "审核失败";
					}
					if (state == -2) {
						newState = "撤销发布";
					}
					//
					if (state1 == 0) {
						newState1 = "";
					}
					if (state1 == 1) {
						newState1 = "待审核";
					}
						if (state1 == 2) {
						newState1 = "待发布";
					}
					if (state1 == 3) {
						newState1 = "已发布";
					}
					if (state1 == 10) {
						newState1 = "有更新";
					}
					if (state1 == -1) {
						newState1 = "审核失败";
					}
					if (state1 == -2) {
						newState1 = "撤销发布";
					}
					html +=`<tr >
						<td><input class="dp_chk" type="checkbox" oid="${key.id}"/></td>
						<td >${key.id}</td>
						<td >${key.title}</td>
						<td >${newState}</td>
						<td >${key.sid}</td>
						<td >${key.stitle}</td>
						<td >${newState1}</td>
						<td >${key.cname}</td>
						<td >${key.type}</td>
						<td >${key.top?"是":"否"}</td>
						<td >${key.recommend?"是":"否"}</td>
						<td >${key.ulike}</td>
						<td >${key.pass}</td>
						<td >${key.comment}</td>
						<td >${key.username?key.username:""}</td>
						<td >${key.createtime?key.createtime:""}</td>
						<td >${key.updatetime?key.updatetime:""}</td>
						<td >${key.atime?key.atime:""}</td>
						<td >${key.rtime?key.rtime:""}</td>
						<td >${key.source}</td>
						<td >${key.author}</td>
						<td >${key.stime}</td>
					</tr>`;
				})
				$(".dp_list").append(html);
			}
		}

	})
	//分页

}
//修改提交的数据
$(function (){
	$(".dp_xg").click(function (){
		var dp_zuozhe = $(".dp_zuozhe").val();//作者
		var dp_laiyuan = $(".dp_laiyuan").val();//来源
		var dp_wangzhi = $(".dp_wangzhi").val();//网址
		var dp_leibie = $(".dp_leibie").val();//短评类别
		//var dp_duanti = $(".dp_duanti").val();//专题类别
		var dp_duanti=$("#lunch").val();
		var dp_biaoti = $(".dp_biaoti").val();//包体
		var content = editor.txt.html();//内容
		var arcImg = $(".tu img").attr("fsrc")||"";//封面图
		var fbs_text = $(".fbs_text").val();//发布时间
		if (arcImg==""){
			zeroModal.alert('请选择封面图片');
			return false;
		}
		var json = {
			"author": dp_zuozhe,
			"content": content,
			"id":$(".dp_id").val(),
			"sid": dp_duanti,
			"source": dp_laiyuan,
			"title": dp_biaoti,
			"type": dp_leibie,
			"url": dp_wangzhi,
			"cover":arcImg
		}
		if (fbs_text!=""){
			json.stime = fbs_text + ":00"
		}
		console.log(json, 9999)
		var result = JSON.stringify(json);
		zeroModal.loading(5);
		
		$.ajax({
			url: baseNews + "/api/data/sr/update",
			type: "post",
			headers: { 'Content-Type': 'application/json' },
			dataType: "json",
			data: result,
			success: function (res) {
				console.log(res, 666)
				if (res.code == 0) {
					location.reload();
				} else {
					zeroModal.alert('添加失败');

				}
			}
		})
	})
	//搜索
	$(".dp_state").change(function () {
		jsonList.state = $(this).val();//短评状态
	})
	$(".pd_zt_state").change(function () {
		jsonList.sstate = $(this).val();//专题状态
	})
	$(".dp_pd").change(function () {
		jsonList.cid = $(this).val();//频道id
	})
	$(".search_btn").on('click',function (){
		var dp_name = $(".dp_name").val();//短评名称
		var dp_zt_id = $(".dp_zt_id").val();//专题id
		var dp_zt_name = $(".dp_zt_name").val();//专题名称
		if (dp_name!=""){
			jsonList.title = dp_name
		}
		if (dp_zt_id != "") {
			jsonList.sid = dp_zt_id
		}
		if (dp_zt_name != "") {
			jsonList.stitle  = dp_zt_name
		}
		getList();
		
	})
	//获取频道id
	var jsonl = {};
	var result = JSON.stringify(jsonl);
	$.ajax({
		url: baseNews + "/api/data/channel/list?page=1&rows=200",
		type: "post",
		headers: { 'Content-Type': 'application/json' },
		dataType: "json",
		data: result,
		success: function (res) {
			console.log(res, 6666);
			if (res.code == 0) {
				var obj = $(".dp_pd");
				$(res.data.list).each(function (index, key) {
					obj.append("<option value='" + key.id + "'>" + key.cname + "</option>");
				})
			}
		}
	})
})

// 图片上传的。。
function sendImg(obj, msrc) {
	var formData = new FormData();
	formData.append('file', msrc);
	$.ajax({
		url: baseNews + '/oss/upload/img',
		data: formData,
		type: "post",
		cache: false,
		processData: false,
		contentType: false,
		success: function (res) {
			console.log(res);
			if (res.code == 0) {
				$(obj).attr("fsrc", res.data);
			} else {
				alert("上传失败");
				return false;
			}
		}

	})
}

$(function (){
	$('.file').on('change', function (e) {
		var e = e || event;
		var reader = new FileReader();
		console.log(reader)
		reader.readAsDataURL($('.file')[0].files[0]);//发起异步请
		//上传图片1
		sendImg('.tu img', $('.file')[0].files[0])
		reader.onload = function () {
			var src = this.result
			$('.tu img').attr('src', src)
		}
	})
})